package cn.jizi.controller;

import cn.jizi.entity.SysMenu;
import cn.jizi.entity.SysUser;
import cn.jizi.entity.TreeNode;
import cn.jizi.service.MenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.constructor.BaseConstructor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 宋朝阳
 * @create 2020 -11 -27 16:25
 */
@Controller
@RequestMapping("/sel")
public class MenuController extends BaseConstructor {
    @Autowired
    private MenuService menuService;

    //跳转到登录页面
    @ApiOperation(value = "跳转页面")
    @GetMapping("toLogin")
    public String toLogin(){
        return "view/login";
    }

    /**
     *   登录
     * @param model
     * @param request
     * @param loginname   用户姓名
     * @param pwd    用户密码
     * @return
     */
    @ApiOperation(value = "登录",notes = "查询是否存在这个用户")
    @PostMapping("login")
    public String login(Model model, HttpServletRequest request,String loginname,String pwd){

        //第一步：建立subject
        Subject subject = SecurityUtils.getSubject();
        //第二步：封装token  凭证
        UsernamePasswordToken token = new UsernamePasswordToken(loginname, pwd);
        //第三步：登陆
        try {
            //只要能通过认证就能通过了
            subject.login(token);
            SysUser users=menuService.loginname(loginname);
            //把user放进session
            request.getSession().setAttribute("user", users);
            model.addAttribute("loginname",users.getRealname());
            model.addAttribute("id",users.getUserid());
            //把yonghu放进session
            request.getSession().setAttribute("yonghu", users.getRealname());
            return "view/index";//跳转首页
        } catch (UnknownAccountException e) {
            //用户为空或不存在
            model.addAttribute("msg", "不存在这样的用户!");
            return "view/login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码输入不对!");
            return "view/login";
        }
    }

    /*
     * 加载首页左边导航栏
     * */
    @RequestMapping("toTreeLoad")
    @ResponseBody
    public List<TreeNode> toTreeLoad(HttpServletRequest request) {
        //获取用户登陆id根据不同的用户有不同菜单
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        ///获取用户登陆id根据不同的用户有不同菜单
        List<SysMenu> list = menuService.queryMenuByUid(user.getUserid());
        //创建list集合
        //把list放入nodes
        List<TreeNode> nodes = new ArrayList<>();
        for (SysMenu menus : list) {
            Boolean spread = false;
            Integer id = menus.getId();
            Integer pid = menus.getPid();
            String title = menus.getTitle();
            String icon = menus.getIcon();
            String href = menus.getHref();
            if (menus.getSpread()==1){
                spread =  true;
            }
            String target = menus.getTarget();
            nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
        }
        //组装菜单
        List<TreeNode> treeNodes = new ArrayList<>();
        //n1.getPid() == 1 为父级菜单
        for (TreeNode n1 : nodes) {
            if (n1.getPid() == 1) {
                treeNodes.add(n1);
            }
            //将n2放入n1的子级中  id为子级  测试
            for (TreeNode n2 : nodes) {
                if (n2.getPid() == n1.getId()) {
                    n1.getChildren().add(n2);
                }
            }
        }
        return treeNodes;
    }

}
